﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
    }).use(['element', 'form', 'flow', 'table', 'util', 'layedit', 'common'], function () {
    var $ = layui.$,
        element = layui.element,
        util = layui.util,
        flow = layui.flow,
        table = layui.table,
        layedit = layui.layedit,
        common = layui.common,
        jhy = layui.data("jhy");
    var tableNote = null;
    //必须在框架内使用
    common.noneIframe();

    function tableload(note_para) {
         tableNote = table.render({
            elem: '#tableNote',
            url: '/Ashx/own.ashx', //数据接口
            method: 'post',
            page: {
                limit: 20,
                layout: ['prev', 'page', 'next', 'count', 'limit']
            },
            where: { functionname: 'jhy_note_query', note_para: note_para },
            cols: [[ //表头      
                { field: 'note_title', title: '标题', style: "cursor:pointer" }
            ]]
        });
    }
    tableload("");

    //新增和查询
    util.fixbar({
        bar1: "&#xe642;",
        bar2: "&#xe615;",
        bgcolor: "#009688",
        click: function (type) {
            if (type === 'bar1') {//新增
                var div = "<div style='padding:5px;'>\
                <input type=\"text\" id=\"note_title\" name=\"title\"  placeholder=\"请输入标题\" class='layui-input'style='margin-bottom:5px;'/>\
                <textarea name=\"content\" id='note_content' placeholder=\"请输入内容\" class=\"layui-textarea\"></textarea>\
                </div >";
                var layeditnote = null;
                parent.layui.layer.open({
                    type: 1,
                    title: "记一笔",
                    skin: "jhy-layer-css",
                    area: ["100%", "100%"],
                    btn: ["取消", "保存"],
                    content: div,
                    success: function (layero, index) {
                        var h = $(window).height() - 103;
                        layeditnote = parent.layui.layedit.build('note_content', {
                            height: h
                        });
                    }, yes: function (index, layero) {
                        parent.layui.layer.close(index);
                    }, btn2: function (index, layero) {
                        var note_title = $.trim(layero.find("#note_title").val());
                        var note_content = parent.layui.layedit.getContent(layeditnote);
                       
                        if (note_content == "") {
                            parent.layui.layer.msg("内容不能为空");
                            return false;
                        } else {
                            if (note_title == "") {
                                note_content_nohtml = parent.layui.layedit.getText(layeditnote);                               
                                if (note_content_nohtml.length < 20) {
                                    note_title = note_content_nohtml;                                   
                                } else {
                                    note_title = note_content_nohtml.substring(0, 20);                                   
                                }
                            }
                        }

                        //提交数据
                        $.ajax({
                            type: "post",
                            url: "/Ashx/own.ashx",
                            data: { functionname: "jhy_note_add", note_title: note_title, note_content:escape(note_content) },
                            success: function (msg) {
                                if (msg == "true") {
                                    parent.layui.layer.msg("成功");
                                    tableNote.reload();
                                } else {
                                    parent.layui.layer.msg("失败");
                                    tableNote.reload();
                                }
                                parent.layui.layer.close(index);
                            }
                        });
                    }
                });
            } else if (type === 'bar2') {//查询
                layer.prompt({
                    formType: 0,
                    title: '输入查询条件'
                }, function (value, index, elem) {                   
                    tableload(value);
                    layer.close(index);
                });
            }
        }
    });

    //显示及修改操作
    table.on('row(tableNote)', function (obj) {
        var data = obj.data,
            show = "<div class='noteBox layui-text' style='padding:5px;'></div > ",
            div = "<div style='padding:5px;'>\
                <input type=\"text\" id=\"note_id\" name=\"id\"  placeholder=\"请输入ID\" class='layui-input layui-hide' disabled style='margin-bottom:5px;'/>\
                <input type=\"text\" id=\"note_title\" name=\"title\"  placeholder=\"请输入标题\" class='layui-input'style='margin-bottom:5px;'/>\
                <textarea name=\"content\" id='note_content' placeholder=\"请输入内容\" class=\"layui-textarea\"></textarea>\
                </div >",
            layeditnote=null;

        parent.layui.layer.open({
            title: false,
            type:1,
            content: show,
            closeBtn: false,
            skin: "jhy-layer-css",
            area: ["100%", "100%"],
            btn: ["删除", "修改", "保存", "取消"],
            success: function (layero, index) {
                layero.find("div.noteBox").html("<h2>" + data.note_title + "</h2><br>" + unescape(data.note_content));
                layero.find("div.layui-layer-btn").find(".layui-layer-btn2").addClass("layui-hide");
            }, yes: function (index, layero) {
               
                parent.layui.layer.confirm('确认删除【' + data.note_title + '】?', { icon: 3, title: '提示' }, function (index2) {
                    //do something
                   
                    $.ajax({
                        type: "post",
                        url: "/Ashx/own.ashx",
                        data: { functionname: "jhy_note_delete", note_id: data.note_id },
                        success: function (msg) {
                            if (msg == "true") {
                                parent.layui.layer.msg("成功");
                                obj.del();
                            } else {
                                parent.layui.layer.msg("失败");
                                tableNote.reload();
                            }
                            parent.layui.layer.close(index2);
                            parent.layui.layer.close(index);
                        }
                    });                    
                });
               
            }, btn2: function (index, layero) {
                layero.find("div.layui-layer-content").html(div);
                layero.find("#note_id").val(data.note_id);
                layero.find("#note_title").val(data.note_title);
                layero.find("#note_content").val(unescape(data.note_content));
                var h = $(window).height() - 103;
                layeditnote = parent.layui.layedit.build('note_content', {
                    height: h
                });
                layero.find("div.layui-layer-btn").find(".layui-layer-btn1").addClass("layui-hide");
                layero.find("div.layui-layer-btn").find(".layui-layer-btn2").removeClass("layui-hide");
                return false;
            }, btn3: function (index, layero) {
                var note_id = $.trim(layero.find("#note_id").val());
                var note_title = $.trim(layero.find("#note_title").val());
                var note_content = parent.layui.layedit.getContent(layeditnote);
                               
                if (note_content == "") {
                    parent.layui.layer.msg("内容不能为空");
                    return false;
                } else {
                    if (note_title == "") {
                        note_content_nohtml = parent.layui.layedit.getText(layeditnote)
                       
                        if (note_content_nohtml.length < 20) {
                            note_title = note_content_nohtml;
                        } else
                        {
                            note_title = note_content_nohtml.substring(0, 20);
                        }
                    }
                }

                //提交数据
                $.ajax({
                    type: "post",
                    url: "/Ashx/own.ashx",
                    data: { functionname: "jhy_note_edit", note_id: note_id, note_title: note_title, note_content: escape(note_content) },
                    success: function (msg) {
                        if (msg == "true") {
                            parent.layui.layer.msg("成功");
                            tableNote.reload();
                        } else {
                            parent.layui.layer.msg("失败");
                            tableNote.reload();
                        }
                        parent.layui.layer.close(index);
                    }
                });

                return false;
            }
            , btn4: function (index, layero) {
                parent.layui.layer.close(index);
            }
        });

    });


});


